package com.example.luyao.myapplication;

import android.app.AlertDialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Map;

public class CustomAdapter extends BaseAdapter{
    private final static String TAG = Constants.LOG_PREFIX + CustomAdapter.class.getCanonicalName();

    private Context context;
    private List<Map<String, String>> listContent;
    private static LayoutInflater inflater = null;
    private WeakReference<AppCompatActivity> activity;
    private int clickStartIntent;
    private int list_type; // 0: bluetooth, 1: lines
    private AlertDialog lineChooseDialog;
    private QuadBezierView coffee_view;

    public CustomAdapter(Context context, List<Map<String, String>> listContent, AppCompatActivity activity,
                         int clickStartIntent, int list_type, AlertDialog lineChooseDialog,
                         QuadBezierView coffee_view) {
        this.context = context;
        this.listContent = listContent;
        this.inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.activity = new WeakReference<>(activity);
        this.clickStartIntent = clickStartIntent;
        this.list_type = list_type;
        this.lineChooseDialog = lineChooseDialog;
        this.coffee_view = coffee_view;
    }
    @Override
    public int getCount() {
        return listContent.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View rowView = null;
        if(list_type == 0) {
            rowView = inflater.inflate(R.layout.bluetooth, null);
            TextView name_view = rowView.findViewById(R.id.name);
            String name = listContent.get(position).get("name");
            String mac = listContent.get(position).get("mac").replace(":", "");
            String bonded = listContent.get(position).get("bonded");

            name_view.setText(name + " " + mac + " " + bonded);
            if (clickStartIntent == 100) {
                rowView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String name = listContent.get(position).get("name");
                        String mac = listContent.get(position).get("mac");
                        String bonded = listContent.get(position).get("bonded");

                        Log.e(TAG, "You Clicked " + name);
                        ((BluetoothActivity) (activity.get())).start_control_activity(name, mac, bonded);
                    }
                });
            }
        } else if(list_type == 1){
            rowView = inflater.inflate(R.layout.line, null);
            TextView name_view = rowView.findViewById(R.id.name);
            name_view.setText(listContent.get(position).get("name"));
            rowView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e(TAG, "You Clicked " + position);
                    //GlobalParameter.setLine_index(position);
                    lineChooseDialog.dismiss();
                    coffee_view.changeTemperatureData(position);
                    coffee_view.invalidate();
                }
            });
        }
        return rowView;
    }
}